<template>
  <div class="exam-paper-config">
    <div class="paper-header">
      <div class="paper-info">
        <div class="info-item">
          <span class="label">组卷方式：</span>
          <span class="value">抽题组卷</span>
        </div>
        <div class="info-item">
          <span class="label">试题总数：</span>
          <span class="value green">{{ totalQuestions }}</span>
        </div>
        <div class="info-item">
          <span class="label">试卷总分：</span>
          <span class="value green">{{ totalScore }}</span>
        </div>
      </div>
      <button class="save-button">保存试卷</button>
    </div>

    <div v-for="(section, index) in questionSections" :key="index" class="question-section">
      <div class="section-title">{{ section.title }}</div>
      <div class="divider"></div>

      <div class="section-info">
        <div class="count-info">
          共 <span class="green">{{ section.count }}</span> 题，
          共 <span class="green">{{ section.score }}</span> 分
        </div>
        <div class="delete-button" @click="deleteSection(index)">
          <i class="delete-icon"></i>
          删除
        </div>
      </div>

      <div class="score-per-question">
        <span>大题标题</span>
        <input type="text"
         :value="section.title"
        @input="updateTitle($event, index)" class="score-input" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed , defineProps ,defineEmits} from 'vue';
import { ElMessage ,ElMessageBox} from 'element-plus';
const props = defineProps(["transformedData"]);
const emit=defineEmits(['update:transformedData',"deletedGrouup"])
// 使用可写计算属性，处理深拷贝和事件触发
const questionSections = computed({
  get() {
    // 深拷贝避免直接修改 props
    return props.transformedData.map(item => ({ ...item }));
  },
  set(newValue) {
    // 通知父组件更新数据
    emit('update:transformedData', newValue);
  }
});
const updateTitle = (event, index) => {
  // 创建新数组避免直接修改计算属性
  const newSections = [...questionSections.value];
  newSections[index].title = event.target.value;
  // 触发计算属性的 setter
  questionSections.value = newSections;
};
// ref([
//   { title: '单选题', count: 4, score: 40, scorePerQuestion: 10.0 },
//   { title: '多选题', count: 2, score: 20, scorePerQuestion: 10.0 },
//   { title: '判断题', count: 3, score: 30, scorePerQuestion: 10.0 },
//   { title: '简答题', count: 1, score: 10, scorePerQuestion: 10.0 }
// ]);






const totalQuestions = computed(() => {
  return questionSections.value.reduce((sum, section) => sum + section.count, 0);
});

const totalScore = computed(() => {
  return questionSections.value.reduce((sum, section) => sum + section.score, 0);
});

const  deleteSection= async (index) =>{
  // 实际应用中可能需要确认对话框
  try {
    await ElMessageBox.confirm('确定要删除这个题目组吗？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    emit('deletedGrouup',index)

    ElMessage.success('题目组删除成功')
  } catch {
    ElMessage.info('已取消删除')
  }
}


const removeQuestionGroup = async (index) => {
  try {
    await ElMessageBox.confirm('确定要删除这个题目组吗？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    exam.questionGroups.splice(index, 1)
    ElMessage.success('题目组删除成功')
  } catch {
    ElMessage.info('已取消删除')
  }
}
</script>




<style scoped>
.exam-paper-config {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: #333;
  position: sticky;
top: 68px;
}

.paper-header {
  margin-bottom: 20px;
}

.paper-info {
  margin-bottom: 15px;
}

.info-item {
  margin-bottom: 8px;
  font-size: 16px;
}

.label {
  font-weight: 500;
}

.value {
  font-weight: 400;
}

.green {
  color: #4cd964;
  font-weight: 500;
}

.save-button {
  width: 100%;
  background-color: #4cd964;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 12px 0;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.save-button:hover {
  background-color: #44c259;
}

.question-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 10px;
}

.divider {
  height: 1px;
  background-color: #eee;
  margin: 10px 0;
}

.section-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.count-info {
  font-size: 14px;
}

.delete-button {
  display: flex;
  align-items: center;
  color: #4cd964;
  cursor: pointer;
  font-size: 14px;
}

.delete-icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%234cd964'%3E%3Cpath d='M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-size: contain;
  margin-right: 4px;
}

.score-per-question {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.score-input {
  width: 60px;
  height: 30px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin: 0 8px;
  text-align: center;
  font-size: 14px;
}

/* 使用Vue 3的组合式API版本 */
</style>
